package com.example.supernote.model.note.commands

import androidx.compose.runtime.Composable
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.TextRange
import androidx.compose.ui.text.input.TextFieldValue
import com.example.supernote.R

fun insertTextAtRange(
    currentTextFieldValue: TextFieldValue,
    textRange: TextRange,
    insertText: List<String>
): TextFieldValue {
    val currentText = currentTextFieldValue.text
    val start = textRange.min
    val end = textRange.max
    val preSubString = insertText[0]
    val postSunString = insertText[1]
    val offsetLength = preSubString.length + postSunString.length

    val newText = buildString {
        append(currentText.substring(0, start))
        append(preSubString)
        append(currentText.substring(start, end))
        append(postSunString)
        append(currentText.substring(end))
    }

    return TextFieldValue(
        text = newText,
        selection = TextRange(start, end + offsetLength)
    )
}
